<?php
namespace Controller;

class MasterController {

	/**
	 * @return String HTML document
	 */
	public static function doControll() {
		$xhtml = "";

		$database = new \Model\Database();
		$database -> Connect(new \Model\DBConfig());
		$loginhandler = new \Model\LoginHandler($database);
		$logincontroller = new LoginController();
		$regcontroller = new RegisterController();
		$objview = new \View\ObjectView();
		$navview = new \View\NavigationView();
		$pageview = new \Common\Page();
		$navView = new \View\NavigationView();
		$objhandler = new \Model\ObjectHandler($database);
		$commenthandler = new \Model\CommentHandler($database);
		$objController = new ObjectController();
		$objListController = new ObjectListController();
		$newObjController = new NewObjectController();
		$login = $logincontroller -> DoControll($loginhandler);

		//Kollar om man kollar på ett inlägg eller inte
		if ($navView -> isViewingObj()) {
			$pco = $objController -> doControll($objhandler, $database);
			$xhtml = $pco -> m_objectHtml;
		} else {
			$objListController = new ObjectListController();
			$xhtml = $objListController -> doControll($database);

		}
		
		if ($navView -> tryingToRegister()) {
			$xhtml = $regcontroller -> DoControll($database);
		}	
		else if ($loginhandler -> IsLoggedIn() == true )
		{
			if ($navView -> addingNewPost()){
				$xhtml = $newObjController -> DoControll($database);
			}
		}
		
		//Stänger databasen
		$database -> Close();

		//Lägger till en stylesheet
		//$stylesheet = $pageview -> AddStyleSheet('Css/style.css');

		//Returnerar titel och xhtml till pageView
		return $pageview -> GetHTMLPage("bjudNu.net", $xhtml, $login);
	}

}
